package com.example.afinal.Aboutme;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.afinal.Db.MyHelper;
import com.example.afinal.MainActivity;
import com.example.afinal.R;
import com.example.afinal.bean.User;

public class ChangePwdActivity extends AppCompatActivity {
    private EditText etPwd;
    private Button btnChange;
    private MyHelper myHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        btnChange = (Button) findViewById(R.id.btn_change);
        btnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(etPwd.getText().toString())) {
                    myHelper = new MyHelper(v.getContext());
                    SQLiteDatabase db;
                    db = myHelper.getWritableDatabase();
                    db.execSQL("update users set pwd=? where name=?", new String[]{etPwd.getText().toString(), User.name});
                    Toast.makeText(v.getContext(), "修改密码成功,请重新登陆！", Toast.LENGTH_SHORT).show();
                    db.close();
                    User.name = null;
                    User.pwd = null;
                    User.loginstate = !User.loginstate;
                    Intent login_intent = new Intent(v.getContext(), MainActivity.class);
                    login_intent.putExtra("sta", 3);
                    startActivity(login_intent);
                } else {
                    Toast.makeText(v.getContext(), "请输入密码！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


}
